﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMKhoa:DBInteractionBase
    {
        public string MaKhoa { get; set; }
        public string TenKhoa { get; set; }

        public bool Insert(DMKhoa obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKhoa_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", ConvertNvarcharToVarchar(obj.MaKhoa)));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKhoa", obj.TenKhoa));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMKhoa obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKhoa_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", obj.MaKhoa));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKhoa", obj.TenKhoa));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKhoa_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKhoa_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMKhoa_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMKhoa obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKhoa_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", string.IsNullOrEmpty(obj.MaKhoa) ? "" : obj.MaKhoa));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKhoa", string.IsNullOrEmpty(obj.TenKhoa) ? "" : obj.TenKhoa));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMKhoa_SelectAll: " + ex.Message);
            }
        }
    }
}
